require 'json'
module ActiveResource
  module Formats
    module JsonPaginatingFormat
      extend self

      def extension
        "json"
      end

      def mime_type
        "application/json"
      end

      def encode(hash)
        puts hash.inspect
        hash.to_json
      end

      def decode(json)
        from_json_data(JSON.parse(json))
      end

      private
      # 追加パラメータメソッドを追加
      def from_json_data(data)
        if data.is_a?(Hash) && data.keys.include?('results')
          results = data['results']
          data.keys.dup.reject{|key| key == 'results'}.each do |name|
            eval <<-EVAL
            def results.#{name}
                #{data[name].inspect}
            end
            EVAL
          end
          results
        else
          data
        end
      end

    end
  end
end
